package main

import (
	"encoding/json"
	"fmt"
)

type Person struct {
	Name   string
	Age    int64
	Weight float64
}

func main() {

	p1 := Person{
		Name:   "111",
		Age:    20,
		Weight: 3.14,
	}

	b, err := json.Marshal(p1)
	if err != nil {
		fmt.Printf("json marshal failed, err: %v\n", err)
		return

	}
	fmt.Println(b)        // byte slice [123 34 78 97 109 101 34 58 34 49 49 49 34 44 34 65 103 101 34 58 50 48 44 34 87 101 105 103 104 116 34 58 51 46 49 52 125]
	fmt.Printf("%s\n", b) // {"Name":"111","Age":20,"Weight":3.14}

	var p2 Person
	err = json.Unmarshal(b, &p2)
	if err != nil {
		fmt.Printf("json unmarshal failed, err = %v\n", err)
		return

	}
	fmt.Printf("%#v\n", p2) // %#v 把 struct 具体的字段打印出来  main.Person{Name:"111", Age:20, Weight:3.14}
	fmt.Printf("%v\n", p2)  // {111 20 3.14}

}
